package com.it.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.it.entity.Product;
import com.it.mapper.ProductMapper;
import com.it.service.ProductService;
import com.it.util.ItdragonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ItdragonUtils itdragonUtils;
    @Resource
    private ProductMapper productMapper;


    @Override
    public Page<Product> selectPage(Product entity, int page, int limit) {
        EntityWrapper<Product> searchInfo = new EntityWrapper<>();
        Page<Product> pageInfo = new Page<>(page, limit);
        if (ItdragonUtils.stringIsNotBlack(entity.getName())) {
            searchInfo.like("name", entity.getName());
        }
        if (ItdragonUtils.stringIsNotBlack(entity.getCategoryId())) {
            searchInfo.eq("categoryId", entity.getCategoryId());
        }
        if (ItdragonUtils.stringIsNotBlack(entity.getUuId())) {
            searchInfo.like("uuId", entity.getUuId());
        }
        List<Product> resultList = productMapper.selectPage(pageInfo, searchInfo);
        if (!resultList.isEmpty()) {
            pageInfo.setRecords(resultList);
        }
        return pageInfo;
    }

    @Override
    public boolean insert(Product entity) {
        //添加药品唯一编码
        entity.setUuId(itdragonUtils.getOrderIdByUUId());
        Integer insert = productMapper.insert(entity);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean editById(Product entity) {
        Integer insert = productMapper.updateById(entity);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(String ids) {
        String[] idList = ids.split(",");
        int result = 0;
        for (String s : idList) {
            result = productMapper.deleteById(s);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Product> getList(Product entity) {
        EntityWrapper<Product> wrapper = new EntityWrapper<>();
        if (ItdragonUtils.stringIsNotBlack(entity.getName())) {
            wrapper.like("name", entity.getName());
        }
        if (ItdragonUtils.stringIsNotBlack(entity.getCategoryId())) {
            wrapper.eq("categoryId", entity.getCategoryId());
        }
        List<Product> resultList = productMapper.selectList(wrapper);
        return resultList;
    }

    @Override
    public Product getOne(String id) {
        Product product = productMapper.selectById(id);
        if (product != null) {
            return product;
        }
        return new Product();
    }

}